package com.hkfrich.common.utils;

/**
 * @ClassName RSAUtil
 * @Desscription rsa加密工具类
 * @Author _HeiShuai
 * @Date 2022/9/1 15:58
 * @Version 1.0
 **/


import io.netty.handler.codec.DecoderException;
import org.apache.tomcat.util.codec.binary.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
//import JSEncrypt from 'encryptlong';
//        export default {
//        name:"Home",
//        data() {
//        return {
//        data:{
//        "searchlistarea": "潘河供电所",
//        "searchlistcity": "卢氏县供电公司",
//        "beginTime": "",
//        "endtTme": "",
//        "page": 1,
//        "rows": 50
//        },
//        PUBLIC_KEY:"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhhYZduRyHLuIifFqjCmIRuVvdzdqyTCVOweskEpK8iupVMww2AhqtOpsaZVLTy4Y+wvn+1MNYO9rgZitpTFCgqLkOknKxODb8to7x25lWpjR514D5SaRh2SFDwAuFfUHBEb6wbfu8jngqmL9s0JDb0WzB6JiSdxq9DH1UA+bh+eLdkHNehMf1rJg33wZnI9NQVPv1iVPga8S2c+RWl4f03+bw9NhYcCtaE5LL+yiEPcZDB41Dfwmb5I5sf5J2KMEQ/uSjquBvntcyr2t98ytKeCzXVLyxwsDQwCa3Upj0JhgexqOl9XT1a+vliuTo7dUOpZX3uiV6tKo3cqoB8MKdQIDAQAB",
//        PRIVATE_KEY:"MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMFPa+v52FkSUXvcUnrGI/XzW3EpZRI0s9BCWJ3oNQmEYA5luWW5p8h0uadTIoTyYweFPdH4hveyxlwmS7oefvbIdiP+o+QIYW/R4Wjsb4Yl8MhR4PJqUE3RCy6IT9fM8ckG4kN9ECs6Ja8fQFc6/mSl5dJczzJO3k1rWMBhKJD/AgMBAAECgYEAucMakH9dWeryhrYoRHcXo4giPVJsH9ypVt4KzmOQY/7jV7KFQK3x//27UoHfUCak51sxFw9ek7UmTPM4HjikA9LkYeE7S381b4QRvFuf3L6IbMP3ywJnJ8pPr2l5SqQ00W+oKv+w/VmEsyUHr+k4Z+4ik+FheTkVWp566WbqFsECQQDjYaMcaKw3j2Zecl8T6eUe7fdaRMIzp/gcpPMfT/9rDzIQk+7ORvm1NI9AUmFv/FAlfpuAMrdL2n7p9uznWb7RAkEA2aP934kbXg5bdV0R313MrL+7WTK/qdcYxATUbMsMuWWQBoS5irrt80WCZbG48hpocJavLNjbtrjmUX3CuJBmzwJAOJg8uP10n/+ZQzjEYXh+BszEHDuw+pp8LuT/fnOy5zrJA0dO0RjpXijO3vuiNPVgHXT9z1LQPJkNrb5ACPVVgQJBALPeb4uV0bNrJDUb5RB4ghZnIxv18CcaqNIft7vuGCcFBAIPIRTBprR+RuVq+xHDt3sNXdsvom4h49+Hky1b0ksCQBBwUtVaqH6ztCtwUF1j2c/Zcrt5P/uN7IHAd44K0gIJc1+Csr3qPG+G2yoqRM8KVqLI8Z2ZYn9c+AvEE+L9OQY="
//        }
//        },
//        created(){
//        var jsencrypt = new JSEncrypt()
//        jsencrypt.setPublicKey(this.PUBLIC_KEY)
//        var result = jsencrypt.encrypt(JSON.stringify(this.data))
//        console.log(result);
//        }
//        }
//vue配合encryptlong使用
public class RsaUtils {
    // 加密数据和秘钥的编码方式
    public static final String UTF_8 = "UTF-8";

    // 填充方式
    public static final String RSA_ALGORITHM = "RSA/ECB/PKCS1Padding";
    public static final String RSA_ALGORITHM_NOPADDING = "RSA";
    //私钥
    public static final String PUBLIC_KEY  = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhhYZduRyHLuIifFqjCmIRuVvdzdqyTCVOweskEpK8iupVMww2AhqtOpsaZVLTy4Y+wvn+1MNYO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74u/4+XNK9pada47O8ZqZMYwsyiaeXXkBwxLFZoB/hh2Do1vZSBNZ8lRp++AJQvo6vVOeojwTggOFNo3oBAAcxUKQ4kWCmKAKeCiXXEc4q0OrgTHkHzFsR3KJQz66Iy1+bbwGpbHlks1gYYQKBgQCegUlWmrVE07mk+iZW2WlSl9smh9Ykyj9YpfEBqXEjeE90ugAu7pQnRwZiZKRHm+kqXCNRkxYJM7S8KmMFxYU4wu7IjKetTOJdrLK4j+v1KuRj4mwc5MUFx9RNaOTQEt+hyHuldRfgM9zmadKGndP1Psk15JTAaHEw/miLNeMalQKBgHGL36D2RNngLE5IaPzZS5SHmpZJDiwIlaOpE63v/zF+lizAPY1rmOFkn4SUHIGD/+EYPd8IFl2+5+PVOYiUvRG5g61GXJW9lMEiS9JSgM8uWbfQhfTrbU9jmiNEcyrd8z0yzsBQTfZQ4lyeX1mcL1bjrYV37KcYquxkPbn49sXBAoGACsM4mZrTqCLAPkWGCOumTSdmiA09Ky5YcsqBvN0mg4QN0R04Gfe1M52M/21aAkrB4FB1TXqxSlqqcQRnyOKKkzoZQKDcWdYYoMezMvkCypjo/SdItAWMo0HYVpFCyCYf7TpRuwBiaKTc4QBafS2GUOa8ft5Qg2gUF4GO07ULAKkCgYEAiNWerNqPhaDgnlOU0DoaJzqypPf2/Zoef+t3msx1YbFt+Gje2wnzMllPoZZgYPquhPKsZnhW91JTcR5zaPXtnnLhs7CpQ3B570n5+LCzGv0cQHLEPb8oGiLXMm0wSCctbGCPobqUy2uZFl8N+kRufXsrGWko1zsvwTbrsmBiviI=";


    /**
     * 非对称加密，根据公钥和原始内容产生加密内容
     */
    private static String encryptRSA(Key key, String plainText)
            throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException,
            BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException {
        Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return Base64.encodeBase64(cipher.doFinal(plainText.getBytes(UTF_8))).toString();
    }

    /**
     * 根据私钥和加密内容产生原始内容
     */
    private static String decryptRSA(Key key, String content) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, DecoderException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException, InvalidAlgorithmParameterException {
        Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] contentArry = content.getBytes();
        return new String(cipher.doFinal(Base64.decodeBase64(contentArry)), UTF_8);
    }

    /**
     * 创建RSA的公钥和私钥示例 将生成的公钥和私钥用Base64编码后打印出来
     * @throws NoSuchAlgorithmException
     */
    public static void createKeyPairs() throws NoSuchAlgorithmException {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
        String publicKeyString = new String(Base64.encodeBase64(publicKey.getEncoded()));
        String privateKeyString = new String(Base64.encodeBase64((privateKey.getEncoded())));
        System.out.println("公钥"+publicKeyString);
        System.out.println("私钥"+privateKeyString);
    }

    /**
     *  Description:默认的RSA解密方法 一般用来解密 参数 小数据
     */
    public static String decryptRSADefault(String privateKeyStr,String data) {
        try{
            KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM_NOPADDING);
            byte[] privateKeyArray = privateKeyStr.getBytes();
            byte[] dataArray = data.getBytes();
            PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyArray));
            PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
            Cipher cipher = Cipher.getInstance(RSA_ALGORITHM_NOPADDING);
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            return new String(cipher.doFinal(Base64.decodeBase64(dataArray)), UTF_8);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }

    public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException, DecoderException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, UnsupportedEncodingException, BadPaddingException, InvalidKeyException {

String pri = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMFPa+v52FkSUXvcUnrGI/XzW3EpZRI0s9BCWJ3oNQmEYA5luWW5p8h0uadTIoTyYweFPdH4hveyxlwmS7oefvbIdiP+o+QIYW/R4Wjsb4Yl8MhR4PJqUE3RCy6IT9fM8ckG4kN9ECs6Ja8fQFc6/mSl5dJczzJO3k1rWMBhKJD/AgMBAAECgYEAucMakH9dWeryhrYoRHcXo4giPVJsH9ypVt4KzmOQY/7jV7KFQK3x//27UoHfUCak51sxFw9ek7UmTPM4HjikA9LkYeE7S381b4QRvFuf3L6IbMP3ywJnJ8pPr2l5SqQ00W+oKv+w/VmEsyUHr+k4Z+4ik+FheTkVWp566WbqFsECQQDjYaMcaKw3j2Zecl8T6eUe7fdaRMIzp/gcpPMfT/9rDzIQk+7ORvm1NI9AUmFv/FAlfpuAMrdL2n7p9uznWb7RAkEA2aP934kbXg5bdV0R313MrL+7WTK/qdcYxATUbMsMuWWQBoS5irrt80WCZbG48hpocJavLNjbtrjmUX3CuJBmzwJAOJg8uP10n/+ZQzjEYXh+BszEHDuw+pp8LuT/fnOy5zrJA0dO0RjpXijO3vuiNPVgHXT9z1LQPJkNrb5ACPVVgQJBALPeb4uV0bNrJDUb5RB4ghZnIxv18CcaqNIft7vuGCcFBAIPIRTBprR+RuVq+xHDt3sNXdsvom4h49+Hky1b0ksCQBBwUtVaqH6ztCtwUF1j2c/Zcrt5P/uN7IHAd44K0gIJc1+Csr3qPG+G2yoqRM8KVqLI8Z2ZYn9c+AvEE+L9OQY=";
String data = "fF0HXuKrOhQ3fpZ1H8fph1be03CMrxnSWCwjd4cWoFWjd1afKzJtiqdjE85AjqR3EgJG5puwL+uat/XCInioUzKHHDFBFNs+soYmQ9FSfaoHpPPYpVtMXKBJ0fPDfTacogUr381YoK0842clmNqzQloL1gIp/DBAQZNu2agG8qk=";
System.out.println(decryptRSADefault(pri,data));


    }
}